package com.tuerks.sbahn;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends ListActivity implements OnClickListener,
		OnItemLongClickListener {

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		showStations();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.mainmenu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.addStation:
			startActivity(new Intent(this, StationPickerActivity.class));
			return true;
		case R.id.preferences:
			startActivity(new Intent(this, MyPreferenceActivity.class));
			return true;
		case 1:
			startActivity(new Intent(this, FilterActivity.class));
			return true;
		}
		return false;
	}

	public void onResume(Bundle savedInstanceState) {
		// super.onResume(savedInstanceState);
		showStations();
		Log.v("s-bahn", "resuming");

	}

	public void showStations() {
		SharedPreferences myPrefs = this.getSharedPreferences("myPrefs",
				MODE_WORLD_READABLE);
		String stations = myPrefs.getString("stations", null);

		try {
			if (stations == null) {
				startActivity(new Intent(this, StationPickerActivity.class));
			}

			else if (stations != null) {
				JSONObject jO = new JSONObject(stations);
				JSONArray jA = jO.getJSONArray("stations");
				List<String> list = new ArrayList<String>();
				for (int i = 0; i < jA.length(); i++) {
					list.add(jA.getString(i));
				}
				final String[] sA = new String[jA.length()];
				final String[] sAdecoded = new String[jA.length()];
				list.toArray(sA);
				for (int i = 0; i < sA.length; i++) {
					sAdecoded[i] = URLDecoder.decode(sA[i]);
					sAdecoded[i] = sAdecoded[i].substring(0,
							sAdecoded[i].indexOf("#"));
				}

				setListAdapter(new ArrayAdapter<String>(this,
						R.layout.stationoverviewactivity, sAdecoded));

				ListView lv = getListView();
				lv.setTextFilterEnabled(true);
				lv.setFastScrollEnabled(true);

				lv.setOnItemClickListener(new OnItemClickListener() {
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						Uri uri = null;
						uri = Uri.parse("http://www.tuerks.com/?station="
								+ sA[position] + "&action=add");
						Intent newActivity0 = new Intent("OVERVIEW", uri,
								MainActivity.this, ShowStationActivity.class);
						startActivity(newActivity0);
					}
				});

				lv.setOnItemLongClickListener(new OnItemLongClickListener() {
					public boolean onItemLongClick(AdapterView<?> partent,
							View view, int position, long id) {
						Uri uri = null;
						Log.v("s-bahn", "long lcick detected");
						uri = Uri.parse("http://www.tuerks.com/?station="
								+ position + "&action=delete");
						Intent newActivity0 = new Intent("STATION_REMOVAL",
								uri, MainActivity.this,
								StationRemovalActivity.class);
						startActivity(newActivity0);
						return true;

					}
				});

			}

		} catch (JSONException e) {

			e.printStackTrace();
		}
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

	public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		// TODO Auto-generated method stub
		return false;
	}

}
